Skip to content

共享库

共享库

目录

[toc]

环境

  • 实验环境
bash
jenkins/jenkins:2.346.3-2-lts-jdk11gitee仓库/github仓库/gitlab仓库都行
  • 实验软件

链接:https:varsrsources

3.将函数写入共享库中

1、创建resources/config/config.json文件

image-20230410133719423

json
{"id":100,"name":"devops"}

2、创建src/og/devops/Common.groovy文件

image-20230411060852834

groovy
packageorg.devopsdefGetUserNameByID(users,id){for(i inusers){if(i["id"] ==id){returni["name"]}}return"null"}

3、创建vars/GetUserName.groovy文件

image-20230411061059239

groovy
defGetUserNameByID(users,id){for(i inusers){if(i["id"] ==id){returni["name"]}}return"null"}

提交:(这里以main分支提交)

image-20230411061242543

image-20230411061306033

4.共享库的定义

image-20230411061858944

Jenkins系统配置 ->Global Pipeline Libraries

首先,我们为共享库设置一个名称 mylib自定义,无需与gitlab仓库一致),注意这个名称后续在Jenkinsfile中引用。 再设置一个默认的版本,这里的版本是分支的名称。我默认配置的是main版本。(github默认版本必须是main)

image-20230411062235324

⚠️ 注意:

  • 这里的Name不一定和刚才创建的仓库名称一样,只是一个别名而已;
  • 这里的Default version填的是仓库的分支名称;
  • 共享库是可以配置多个的!
  • 接下来我们配置**共享库的仓库地址,**我的仓库在gitlab中,所以这里我填写的是Git的方式。如果仓库是私有的方式,需要在jenkins的凭据中添加一个账号用于下载共享库。

image-20230411062317360

image-20230411062301224

点击Save

3、共享库使用

image-20230411195524142

测试1:基础使用共享库

  • 编写Jenkinsfile代码,并提交:

image-20230411063524144

groovy
@Library("mylib") _ pipeline {agent anystages{stage("Test"){steps{script{echo "hello world!"}}}}}

提交:

image-20230411063548434


⚠️

==注意:这里出现一个gitlab报错的问题……在定义共享库配置后:说是无法解析刚才的仓库地址==

hudson.plugins.git.GitException:Command "git ls-remote -h -- http:stdout:stderr:fatal:unable to access 'http:defcommon =neworg.devops.Common()users =[["id":1,"name":"jenkins1"],["id":2,"name":"jenkins2"],["id":3,"name":"jenkins3"],]pipeline {agent anystages{stage("Test"){steps{script{echo "hello world!"name =common.GetUserNameByID(users,1)print(name)}}}}}

提交,并运行流水线。

image-20230411074131422

image-20230411074145754

符合预期。

测试3:vars里函数调用

  • 编写jenkins代码

错误版演示:

image-20230411075041137

image-20230411075103115

提交并运行流水线:

image-20230411075203751

可以看到会报错的。

  • 我们再来演示下正确版写法:

image-20230411075831846

call是固定写法:

image-20230411075854045

groovy
@Library("mylib") _ defcommon =neworg.devops.Common()users =[["id":1,"name":"jenkins1"],["id":2,"name":"jenkins2"],["id":3,"name":"jenkins3"],]pipeline {agent anystages{stage("Test"){steps{script{echo "hello world!"name =common.GetUserNameByID(users,1)print(name)echo "vars output demo"name =GetUserName(users,2)print(name)}}}}}
  • 提交并运行:

image-20230411075807332

符合预期。

测试4:资源文件引用

  • 代码

image-20230411165214715

image-20230411165227422

groovy
@Library("mylib") _ defcommon =neworg.devops.Common()users =[["id":1,"name":"jenkins1"],["id":2,"name":"jenkins2"],["id":3,"name":"jenkins3"],]pipeline {agent anystages{stage("Test"){steps{script{echo "src output demo"name =common.GetUserNameByID(users,1)print(name)echo "vars output demo"name =GetUserName(users,2) print(name)echo "get resources"data =libraryResource 'config/config.json'println(data)}}}}}
  • 提交并运行

image-20230411165134649

测试5:处理json数据

  • 安装pipeline util插件(安装了这个插件后,才能使用这些语法)

image-20230411165815653

image-20230411165707838

  • 代码

image-20230411170933536

groovy
@Library("mylib") _ defcommon =neworg.devops.Common()users =[["id":1,"name":"jenkins1"],["id":2,"name":"jenkins2"],["id":3,"name":"jenkins3"],]pipeline {agent anystages{stage("Test"){steps{script{echo "src output demo"name =common.GetUserNameByID(users,1)print(name)echo "vars output demo"name =GetUserName(users,2)print(name)echo "get resources"data =libraryResource 'config/config.json'println(data)data_json =readJSON text:dataprintln(data_json["id"])}}}}}
  • 运行

image-20230411170538769

4、代码汇总

Jenkinsfile

image-20230411212602091

groovy
@Library("mylib") _ defcommon =neworg.devops.Common()users =[["id":1,"name":"jenkins1"],["id":2,"name":"jenkins2"],["id":3,"name":"jenkins3"],]pipeline {agent anystages{stage("Test"){steps{script{echo "src output demo"name =common.GetUserNameByID(users,1)print(name)echo "vars output demo"name =GetUserName(users,2)print(name)echo "get resources"data =libraryResource 'config/config.json'println(data)data_json =readJSON text:dataprintln(data_json["id"])}}}}}

Common.groovy

image-20230411212647282

groovy
packageorg.devopsdefGetUserNameByID(users,id){for(i inusers){if(i["id"] ==id){returni["name"]}}return"null"}

GetUserName.groovy

image-20230411212711289

groovy
defcall(users,id){for(i inusers){if(i["id"] ==id){returni["name"]}}return"null"}

config.json

image-20230411212858260

json
{"id":100,"name":"devops"}

FAQ

TemplatingEngine实践(扩展)🍊

准备JTE代码

准备Librarys Resource仓库:根目录例如gradlemaven都是单独的library, 目录下steps是我们具体定义的功能函数。

tstmp_20230411213132

准备配置Pipeline Template和Configuration Files:

pipeline_config.groovy

groovy
libraries{sonarqubegradle}application_environments{dev {short_name ="t"}test}

Jenkinsfile

groovy
pipeline {agent {label "master"}stages{stage("build"){steps{build(dev.short_name) build_test()}}}}

安装JTE插件

安装Templating Engine插件

tstmp_20230411213246

进入系统配置,配置插件:

tstmp_20230411213301

tstmp_20230411213312

Pipeline测试

创建一条Pipeline

tstmp_20230411213328

ConsoleOutput

bash
Startedbyuserunknownoranonymous[JTE] Obtained Pipeline Configuration File (show)[JTE] Pipeline Configuration Modifications (show)[JTE] Obtained Template (show)[JTE] Loading Library sonarqube (show)[JTE] Loading Library gradle (show)[JTE] Template Primitives are overwriting Jenkins steps with the following names:(show)[Pipeline] Start of Pipeline[Pipeline] nodeRunningonJenkinsin/var/jenkins_home/workspace/demo-jte-pipeline[Pipeline] {[Pipeline] stage[Pipeline] {(build)[JTE][Step - gradle/build.call(String)][Pipeline] echogradlebuild[Pipeline] echot[JTE][Step - gradle/build_test.call()][Pipeline] readJSON[Pipeline] echo{"name":"jenkins","id":"devopsvip"}[Pipeline] }[Pipeline] 
[Pipeline] }[Pipeline] 
[Pipeline] End of PipelineFinished:SUCCESS

参考文档

https:

版权:此文章版权归 One 所有,如有转载,请注明出处!

链接:可点击右上角分享此页面复制文章链接

上次更新时间:

最近更新